package com.jxl.context;

import jakarta.annotation.Priority;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * Spring上下文工具：获取容器中的Bean（@ServerEndpoint类无法直接@Autowired）
 */
@Component
@Priority(Integer.MIN_VALUE) // 最高优先级，确保最早初始化
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    // 静态方法：获取指定类型的Bean
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}